package com.qdxwx.web.controller.user;


import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.user.UserService;
import com.qdxwx.data.service.website.WebsiteProfileService;
import com.qdxwx.models.user.User;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 个人中心  Controller
 * @author developer
 */
@Controller
@RequestMapping("/user")
public class UserInfoController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(UserInfoController.class);

	@Autowired
	private UserService userService;
	@Autowired
	private WebsiteProfileService websiteProfileService;

	/**
	 * 初始化修改用户信息页面
	 */
	@RequestMapping("/info.html")
	public String editUserInfo(HttpServletRequest request, ModelMap mmap) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			logger.info("user not login!");
			return VIEW_ERROR_403;
		}
		mmap.put("user", user);
		mmap.put("index", 0);
		if(TKRequest.isMobileBrowse(request)){
			return getViewPathH5("user/uc-info");
		}
		return getViewPath("ucenter/uc-info");
	}

	@RequestMapping("/pwd.html")
	public String updateUserPwd(HttpServletRequest request, ModelMap mmap) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			logger.info("user not login!");
			return VIEW_ERROR_403;
		}
		mmap.put("user", user);
		mmap.put("index", 1);
		return getViewPath("ucenter/uc-info");
	}

	/**
	 * 修改用户头像
	 */
	@RequestMapping("/update/avatar.json")
	@ResponseBody
	public AjaxResult updatePicImg(HttpServletRequest request) {
		String avatar = request.getParameter("avatar");
		User user = LoginUser.getUser(request);
		if (user == null) {
			return AjaxResult.error("user not login!");
		}
		user.setAvatar(avatar);
		if (user.getUserId() > 0) {
			userService.updateAvatar(user);
			//更新缓存用户
			LoginUser.setUser(request, user);
			return AjaxResult.success();
		} else {
			return AjaxResult.error("头像修改失败");
		}
	}

	/**
	 * 修改用户密码
	 */
	@RequestMapping("/updatePwd.json")
	@ResponseBody
	public AjaxResult updatePwd(HttpServletRequest request) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			return AjaxResult.error("user not login!");
		}

		//原密码
		String nowPassword = request.getParameter("nowPassword") == null ? "" : request.getParameter("nowPassword");
		//新密码
		String newPassword = request.getParameter("newPassword") == null ? "" : request.getParameter("newPassword");
		//确认密码
		String confirmPwd = request.getParameter("confirmPwd") == null ? "" : request.getParameter("confirmPwd");
		if (nowPassword.equals("") || nowPassword.trim().length() == 0) {
			return AjaxResult.error("请输入旧密码！");
		}
		if (!user.getPassword().equals(TKConvert.toMD5(nowPassword))) {
			return AjaxResult.error("旧密码不正确！");
		}
		if (newPassword.equals("") || newPassword.trim().length() == 0) {
			return AjaxResult.error("请输入新密码！");
		}
		if (!TKString.isPassword(newPassword)) {
			return AjaxResult.error("密码只能是数字字母组合且大于等于6位小于等于16位");
		}
		if (!newPassword.equals(confirmPwd)) {
			return AjaxResult.error("新密码和确认密码不一致！");
		}
		user.setPassword(TKConvert.toMD5(newPassword));
		userService.updateUserPwd(user);
		return AjaxResult.success("修改成功");

	}

	/**
	 * 修改用户信息
	 */
	@RequestMapping("/updateUser.json")
	@ResponseBody
	public AjaxResult updateUserInfo(HttpServletRequest request) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			return AjaxResult.error("user not login!");
		}
		String loginName = TKString.queryKey(request.getParameter("loginName"));
		String realName = TKString.queryKey(request.getParameter("realName"));
		String userName = TKString.queryKey(request.getParameter("userName"));
		String email = request.getParameter("email");
		String avatar = request.getParameter("avatar");

		if (!TKString.isEmpty(loginName) && websiteProfileService.hasBadWord(loginName)) {
			return AjaxResult.error("登录名含有敏感词，请重新输入");
		}
		if (!TKString.isEmpty(realName) && websiteProfileService.hasBadWord(realName)) {
			return AjaxResult.error("姓名含有敏感词，请重新输入");
		}
		if (!TKString.isEmpty(userName) && websiteProfileService.hasBadWord(userName)) {
			return AjaxResult.error("昵称含有敏感词，请重新输入");
		}
		if (!TKString.isEmpty(email) && !TKString.isEmail(email)) {
			return AjaxResult.error("邮箱格式不正确，请重新输入！");
		}
		if (!TKString.isEmpty(email)) {
			user.setEmail(email);
		}
		user.setRealName(realName);//姓名
		user.setUserName(userName);//昵称
		user.setSex(TKConvert.toInteger(request.getParameter("sex")));
		if (!TKString.isEmpty(avatar)) {
			user.setAvatar(avatar);
		}
		userService.update(user);//修改基本信息
		//缓存用户
		LoginUser.setUser(request, user);
		return AjaxResult.success("修改成功");
	}
}